package edu.xidian.forum.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.xidian.forum.common.Result;
import edu.xidian.forum.entity.Follow;
import edu.xidian.forum.entity.User;
import edu.xidian.forum.service.FollowService;
import edu.xidian.forum.service.UserService;
import edu.xidian.forum.vo.UserVO;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static edu.xidian.forum.jwt.JwtUtil.USER_NAME;

@RestController
@RequestMapping("/relationship")
public class RelationshipController {
    @Resource
    private UserService userService;
    @Resource
    private FollowService followService;

    @GetMapping("/subscribe/{userId}")
    public Result<Object> handleFollow(@RequestHeader(value = USER_NAME) String userName,
                                       @PathVariable("userId") String parentId) {
        User user = userService.getUser(userName);
        if (parentId.equals(user.getId())) {
            return Result.failed("You can't follow yourself!");
        }
        Follow one = followService.getOne(
                new LambdaQueryWrapper<Follow>()
                        .eq(Follow::getParentId, parentId)
                        .eq(Follow::getFollowerId, user.getId()));
        if (!ObjectUtils.isEmpty(one)) {
            return Result.failed("You have followed!");
        }

        Follow follow = new Follow();
        follow.setParentId(parentId);
        follow.setFollowerId(user.getId());
        followService.save(follow);
        return Result.success("Successfully followed!", null);
    }

    @GetMapping("/unsubscribe/{userId}")
    public Result<Object> handleUnFollow(@RequestHeader(value = USER_NAME) String userName,
                                         @PathVariable("userId") String parentId) {
        User user = userService.getUser(userName);
        Follow one = followService.getOne(
                new LambdaQueryWrapper<Follow>()
                        .eq(Follow::getParentId, parentId)
                        .eq(Follow::getFollowerId, user.getId()));
        if (ObjectUtils.isEmpty(one)) {
            return Result.failed("You have not followed!");
        }
        followService.remove(new LambdaQueryWrapper<Follow>()
                .eq(Follow::getParentId, parentId)
                .eq(Follow::getFollowerId, user.getId()));
        return Result.success("Successfully unfollow!", null);
    }

    @GetMapping("/validate/{topicUserId}")
    public Result<Map<String, Object>> isFollow(@RequestHeader(value = USER_NAME) String userName,
                                                @PathVariable("topicUserId") String topicUserId) {
        User user = userService.getUser(userName);
        Map<String, Object> map = new HashMap<>(16);
        map.put("hasFollow", false);
        if (!ObjectUtils.isEmpty(user)) {
            Follow one = followService.getOne(new LambdaQueryWrapper<Follow>()
                    .eq(Follow::getParentId, topicUserId)
                    .eq(Follow::getFollowerId, user.getId()));
            if (!ObjectUtils.isEmpty(one)) {
                map.put("hasFollow", true);
            }
        }
        return Result.success(map);
    }

    @GetMapping("/follow/{username}")
    public Result<Map<String, Object>> getFollowByName(@PathVariable("username") String username,
                                                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> map = new HashMap<>(16);
        User user = userService.getUser(username);
        Page<UserVO> page = followService.getFollowList(new Page<>(pageNo, size), user.getId());
        map.put("follows", page);
        return Result.success(map);

    }

    @GetMapping("/fan/{username}")
    public Result<Map<String, Object>> getFanByName(@PathVariable("username") String username,
                                                    @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> map = new HashMap<>(16);
        User user = userService.getUser(username);
        Page<UserVO> page = followService.getFanList(new Page<>(pageNo, size), user.getId());
        map.put("fans", page);
        return Result.success(map);

    }
}
